<?php
$title = '客户';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title . '管理'); ?></h1>
<hr>
<div class="row">
    <?php
    $form = $this->form;
    $form->setAttribute('action', $this->url('client/default', array('controller' => 'client', 'action' => 'index')));
    $form->prepare();
    echo $this->form()->openTag($form);
    ?>
    <div class="col-md-2 col-xs-4" style="margin-bottom: 15px">
        <?php echo $this->formRow($form->get('column')); ?>
    </div>
    <div class="col-md-3 col-xs-5" style="margin-left: 2px;">
        <?php echo $this->formRow($form->get('parame')); ?>
    </div>
    <div class="col-md-2 col-xs-2" style="margin-left: 2px;">
        <?php echo $this->formRow($form->get('paging')); ?>
    </div>
    <div class="col-md-1 col-xs-2" style="margin-left: 2px;">
        <?php echo $this->formSubmit($form->get('submit')); ?>
    </div>
    <?php echo $this->form()->closeTag(); ?>
    <a class="btn btn-primary"
       href="<?php echo $this->url('client/default', array('controller' => 'client', 'action' => 'add')); ?>">添加客户</a>
    <div class="col-md-2 col-xs-3" style="margin-bottom: 15px">
        <select id="sort" class="form-control">
            <option value="0">请选择排序方式</option>
            <option value="1">按利润升序</option>
            <option value="2">按利润降序</option>
        </select>
    </div>
</div>
<table id="client-table" class="table table-hover table-striped">
    <tr>
        <th>身份证号</th>
        <th>姓名</th>
        <th>城市</th>
        <th>利润</th>
        <th>星级</th>
        <th>账户</th>
        <th>贷款</th>
        <th>操作</th>
    </tr>
    <?php foreach ($this->clients as $client): ?>
        <tr>
            <td><?php echo $client['client_id']; ?></td>
            <td><?php echo $client['client_name']; ?></td>
            <td><?php echo $client['client_city']; ?></td>
            <td><?php echo $client['account_profit']; ?></td>
            <td><span class="badge" style="background-color: #3e8f3e;"><?php echo $client['star_level']; ?></span></td>
            <td>
                <a href="<?php echo $this->url("client/default", array("controller" => "account", "action" => "index", "id" => $client["client_id"])); ?>">查看</a>
            </td>
            <td>
                <a href="<?php echo $this->url("client/default", array("controller" => "loan", "action" => "index", "id" => $client["client_id"])); ?>">查看</a>
            </td>
            <td>
                <a class="btn btn-default"
                   href="<?php echo $this->url("client/default", array("controller" => "client", "action" => "edit", "id" => $client["client_id"])); ?>">编辑</a>
                <a class="btn btn-danger"
                   href="<?php echo $this->url("client/default", array("controller" => "client", "action" => "delete", "id" => $client["client_id"])); ?>">删除</a>
            </td>
        </tr>
    <?php endforeach; ?>
</table>
<script type="text/javascript">
    $("#sort").change(function () {
        let id = this.value;
        $.ajax({
            type: 'POST',
            url: '/client/client/sort',
            dataType: 'json',
            data: {id: id},
            success: res => {
                // console.log(res);
                let table = '<table id="client-table" class="table table-hover table-striped">\n' +
                    '    <tr>\n' +
                    '        <th>身份证号</th>\n' +
                    '        <th>姓名</th>\n' +
                    '        <th>城市</th>\n' +
                    '        <th>利润</th>\n' +
                    '        <th>星级</th>\n' +
                    '        <th>账户</th>\n' +
                    '        <th>贷款</th>\n' +
                    '        <th>操作</th>\n' +
                    '    </tr>';
                $.each(res, (index, value) => {
                    console.log(value);
                    table +=
                        '<tr>\n' +
                        '  <td>' + value.client_id + ' </td>\n' +
                        '  <td>' + value.client_name + '</td>\n' +
                        '  <td>' + value.client_city + '</td>\n' +
                        '  <td>' + value.account_profit + '</td>\n' +
                        '  <td><span class="badge" style="background-color: #3e8f3e;">' + value.star_level + '</td>\n' +
                        '  <td>\n' +
                        '    <a href="<?php echo $this->url("client/default", array("controller" => "account", "action" => "index", "id" => $client["client_id"])); ?>">查看</a>\n' +
                        '  </td>\n' +
                        '  <td>\n' +
                        '    <a href="<?php echo $this->url("client/default", array("controller" => "loan", "action" => "index", "id" => $client["client_id"])); ?>">查看</a>\n' +
                        '  </td>\n' +
                        '  <td>\n' +
                        '    <a class="btn btn-default"\n' +
                        '      href="<?php echo $this->url("client/default", array("controller" => "client", "action" => "edit", "id" => $client["client_id"])); ?>">编辑</a>\n' +
                        '    <a class="btn btn-danger"\n' +
                        '      href="<?php echo $this->url("client/default", array("controller" => "client", "action" => "delete", "id" => $client["client_id"])); ?>">删除</a>\n' +
                        '    </td>' +
                        '</tr>';
                });
                table += '</table>';
                $("#client-table").html(table);
            },
            fail: err => console.error(err)
        });
    })
    $("#column").val("<?php echo isset($_GET['column']) ? $_GET['column'] : null ?>");
    $("#parame").val("<?php echo isset($_GET['parame']) ? $_GET['parame'] : null ?>");
    $("#submit").click(function () {
        if ($("#column").val() == '') {
            alert('请选择查询项');
            $("#parame").val('');
        }
    });
</script>